package redis

import (
	"gitee.com/ha666/golibs"
	"github.com/garyburd/redigo/redis"
)

//添加/更新元素及score值
func (c *RedisClient) Zadd(key string, score int,member interface{}) error {
	//data, err := json.Marshal(member)
	//if err != nil {
	//	return err
	//}
	_, err := c.do("ZADD", key, score, golibs.ToJson(member))
	return err
}

//获取元素及score值列表
func (c *RedisClient) ZrangesWithScore(key string) (error, [][]byte ){
	//return c.do("ZRANGE", key,0,-1,"WITHSCORES")
	buffer, err := redis.ByteSlices(c.do("ZRANGE", key,"0","-1","WITHSCORES"))
	if err != nil {
		return err,nil
	}
	return  nil,buffer
	//if len(buffer) > 0 {
	//	value:=make([]interface{},len(buffer))
	//	for i,b:=range buffer {
	//		err = json.Unmarshal(b, &value[i])
	//		if err != nil {
	//			return err,nil
	//		}
	//	}
	//	return  err,value
	//}
	//return err,nil
	//return nil,json.Unmarshal(buffer,value)
}

//获取元素列表
func (c *RedisClient) Zranges(key string) (error, [][]byte ){
	//return c.do("ZRANGE", key,0,-1,"WITHSCORES")
	buffer, err := redis.ByteSlices(c.do("ZRANGE", key,"0","-1"))
	if err != nil {
		return err,nil
	}
	return nil,buffer
	//if len(buffer) > 0 {
	//	value:=make([]interface{},len(buffer))
	//	for i,b:=range buffer {
	//		err = json.Unmarshal(b, &value[i])
	//		if err != nil {
	//			return err,nil
	//		}
	//	}
	//	return  err,value
	//}
	//return err,nil
	//return nil,json.Unmarshal(buffer,value)
}

//移除元素及score值
func (c *RedisClient) Zrem(key string,member interface{})error{
	//data, err := json.Marshal(member)
	//if err != nil {
	//	return err
	//}
	_, err:= c.do("ZREM", key,golibs.ToJson(member))
	return err
}


